package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.widget.doOnTextChanged
import com.presto.directo.R
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.ActivityWorkinfoBinding
import com.presto.directo.mvvm.viewmodel.WorkViewModel
import com.presto.directo.mvvm.viewmodel.getViewModel
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*
import ru.whalemare.sheetmenu.SheetMenu

class InfoWorkActivity : BaseActivity() {
    override val rootView: View
        get() = binding.root

    private val binding by lazy { ActivityWorkinfoBinding.inflate(layoutInflater) }
    private val views by lazy { arrayListOf(binding.tvEducation, binding.tvJobIncome, binding.tvJobType, binding.tvJobYears, binding.tvWageFrequency, binding.tvEmail) }
    private val viewModel: WorkViewModel by lazy { getViewModel() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_work_page.track(this)
        viewModel.infoLiveData.observe(this) {
            showInfo(binding.tvEducation, R.array.array_education, viewModel.education)
            showInfo(binding.tvJobIncome, R.array.array_job_income, viewModel.salary)
            if (viewModel.work_type == 1) {
                showInfo(binding.tvJobType, R.array.array_job_type_work, viewModel.industry)
            } else if (viewModel.work_type == 3) {
                showInfo(binding.tvJobType, R.array.array_job_type_free, viewModel.industry, minusNum = 31)
            } else {
                showInfo(binding.tvJobType, R.array.array_job_type, viewModel.work_type)
            }
            showInfo(binding.tvJobYears, R.array.array_job_years, viewModel.work_experience)
            showInfo(binding.tvWageFrequency, R.array.array_wage_frequency, viewModel.payroll_cycle)
            binding.tvEmail.setText(it.email)
        }
        viewModel.getInfo()
    }

    override fun initEvent() {
        views.forEach {
            it.doOnTextChanged { text, start, before, count -> updateBtn() }
        }
        setClick(binding.tvEducation, R.array.array_education)
        setClick(binding.tvJobIncome, R.array.array_job_income)
        binding.tvJobType.setOnClickListener {
            SheetMenu(
                actions = resources.getStringArray(R.array.array_job_type).asList(),
                onClick = {
                    val index = it.id
                    when (it.id) {
                        0, 2 -> SheetMenu(
                            actions = resources.getStringArray(if (it.id == 0) R.array.array_job_type_work else R.array.array_job_type_free).asList(),
                            onClick = {
                                viewModel.work_type = index + 1
                                viewModel.industry = if (index == 0) it.id + 1 else it.id + 31
                                binding.tvJobType.text = it.title
                            },
                            layoutProvider = MyLayoutProvider()
                        ).show(this)
                        else -> {
                            viewModel.work_type = index + 1
                            viewModel.industry = -1
                            binding.tvJobType.text = it.title
                        }
                    }
                },
                layoutProvider = MyLayoutProvider()
            ).show(this)
        }
        setClick(binding.tvJobYears, R.array.array_job_years)
        setClick(binding.tvWageFrequency, R.array.array_wage_frequency)
        binding.btnNext.setOnClickListener {
            Af.click_submit_workInfo.track(this)
            val map = getHttpMap()
            map["education"] = viewModel.education
            map["email"] = binding.tvEmail.text.toString()
            map["work_type"] = viewModel.work_type
            if (viewModel.industry > 0) {
                map["industry"] = viewModel.industry
            }
            map["salary"] = viewModel.salary
            map["work_experience"] = viewModel.work_experience
            map["payroll_cycle"] = viewModel.payroll_cycle
            getApiService().saveWorkInfo(map.sign(16))
                .applySchedulers()
                .doOnSubscribe { showProgress() }
                .doFinally { dismissProgress() }
                .getResponse(this, {
                    it.getResultOrNull()
                    Af.success_submit_workInfo.track(this)
                    startActivity(Intent(this, InfoQuestionActivity::class.java))
                    finish()
                })
        }
    }

    private fun setClick(textView: TextView, res: Int) {
        textView.setOnClickListener {
            SheetMenu(
                actions = resources.getStringArray(res).asList(),
                onClick = {
                    textView.text = it.title
                    when (textView) {
                        binding.tvEducation -> viewModel.education = it.id + 1
                        binding.tvJobIncome -> viewModel.salary = it.id + 1
                        binding.tvJobYears -> viewModel.work_experience = it.id + 1
                        binding.tvWageFrequency -> viewModel.payroll_cycle = it.id + 1
                    }
                },
                layoutProvider = MyLayoutProvider()
            ).show(this)
        }
    }

    override fun updateBtn() {
        binding.btnNext.isEnabled = views.all { it.text.isNotBlank() }
    }

}